home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr52 / ntx2.zip / NTXPOS.C next >
Text File  |  1993-04-13  |  5KB  |  221 lines

  1. //.............................................................................
  2. //
  3. //   Program Name: NTXPOS.C          Copyright: RCM Software Pty. Ltd.                                   
  4. //   Date Created: 27/04/91           Language: Microsoft C 5.1                                         
  5. //   Time Created: 17:49:22             Author: Graham D. McKechnie                       
  6. //
  7. //   Modified 9/25/92 7:00 pm
  8. //   by Brian Marasca
  9. //   Use index handle rather than file name.
  10. //   IndexOrd() is now passed as parameter 1.
  11. // 
  12. //   Returns index record number
  13. //   eg. nNtxPos := NtxPos( IndexOrd(), RECNO() )
  14. //.............................................................................
  15.         
  16. #include "extend.h"
  17. #include "stdio.h"
  18.  
  19. #define open  _topen                // Use Clipper's internals
  20. #define close _tclose               // Don't have to link LLIBCA
  21. #define lseek _tlseek
  22. #define read  _tread
  23.  
  24. extern int  _topen( char*, int);
  25. extern int  _tclose( int );
  26. extern long _tlseek( int, long, int);
  27. extern int  _tread( int, char*, int);
  28.  
  29. #define BUFF_SIZE   1024
  30. #define ERROR       -1
  31. #define MAX_KEY     256
  32. #define O_RDONLY    0x0000  // open for reading only 
  33. #define O_BINARY    0x8000  // file mode is binary (untranslated) 
  34.  
  35.  
  36. // Index structures 
  37. typedef struct
  38. {
  39.    unsigned  uSign;
  40.    unsigned  uVersion;
  41.    long      lRoot;
  42.    long      lNextPage;
  43.    unsigned  uItemSize;
  44.    unsigned  uKeySize;
  45.    unsigned  uKeyDec;
  46.    unsigned  uMaxItem;
  47.    unsigned  uHalfPage;
  48.    char      cKeyExpr[MAX_KEY];
  49.    Boolean   bUnique;
  50. } NTXHEADER;
  51.  
  52.  
  53. typedef struct
  54. {
  55.    long lPage;
  56.    long lRecNo;
  57.    char cKey;
  58. } ITEM;
  59.  
  60.  
  61. typedef struct
  62. {
  63.    unsigned uCount;
  64.    unsigned uRef;
  65. } BUFFER;
  66.  
  67.  
  68.  
  69. static Boolean bError = FALSE;
  70. static Boolean bFound = FALSE;
  71.  
  72. static long lRetVal;            // Return value
  73. static long lNtxPos;            // Index position
  74. static int  nNtxHandle;         // Index file handle
  75.  
  76.  
  77. CLIPPER ntxpos()
  78. {
  79.    long lRecNo;
  80.    int nNtxOrd;
  81.    long nNtxPtr;
  82.  
  83.    NTXHEADER NtxHeader;
  84.  
  85.    // Check the parameters passed from Clipper
  86.    if ( PCOUNT != 2 )
  87.    {
  88.       _retni(-1);
  89.       return;
  90.    }
  91.  
  92.    if ( ! (ISNUM(1) && ISNUM(2) ) )
  93.    {
  94.       _retni(-1);
  95.       return;
  96.    }
  97.  
  98.    nNtxOrd  = _parni(1);    // indexord() to go by
  99.    lRecNo   = _parnl(2);    // RECNO() we are looking for
  100.  
  101.    lRetVal  = 0L;           // Set these each time
  102.    lNtxPos  = 0L;
  103.    bFound   = FALSE;
  104.  
  105.    if ( ( nNtxHandle = getNtxHandle ( nNtxOrd ) ) != -1 )
  106.    {
  107.         /* save the ntx file position and reset it to the top */
  108.         nNtxPtr = _tlseek( nNtxHandle, 0L, SEEK_CUR );
  109.         _tlseek( nNtxHandle, 0L, SEEK_SET );
  110.  
  111.         /* read the header */
  112.         if ( read ( nNtxHandle, (char *) &NtxHeader, sizeof(NtxHeader) )
  113.                        != sizeof(NtxHeader) )
  114.         goto ERROR_EXIT; 
  115.  
  116.         /* start the traversal from root */
  117.         DumpPage( NtxHeader.lRoot, lRecNo );
  118.  
  119.         if (bError)
  120.         {
  121.              ERROR_EXIT:
  122.              bError = TRUE;
  123.         }
  124.  
  125.         /* restore the ntx file position */
  126.         _tlseek( nNtxHandle, nNtxPtr, SEEK_SET );
  127.    }
  128.  
  129.    _retnl( lRetVal );
  130.  
  131. }
  132.  
  133.  
  134.  
  135. DumpPage(long lPageOffSet, long lRecNo)
  136.  
  137. {
  138.    char     *cPage;
  139.    ITEM     *item;
  140.    BUFFER   *buffer;
  141.    unsigned i;
  142.    unsigned *uItemRef;
  143.  
  144.    /* allocate page */
  145.    cPage = _xalloc(BUFF_SIZE);
  146.  
  147.    /* move to this position in the file */
  148.    if( lseek( nNtxHandle, lPageOffSet, 0 ) != (long)lPageOffSet )
  149.       goto DUMP_EXIT;
  150.  
  151.    /* read the page */
  152.    if( read( nNtxHandle, cPage, BUFF_SIZE) != BUFF_SIZE )
  153.       goto DUMP_EXIT;
  154.  
  155.  
  156.    buffer   = (BUFFER *) cPage;
  157.    uItemRef = &buffer -> uRef;
  158.  
  159.  
  160.    for (i = 0; i < buffer -> uCount; i++)
  161.    {
  162.       item = (ITEM *) &cPage[ uItemRef[ i ] ];
  163.  
  164.       if ( ! bFound )
  165.       {
  166.          if ( item -> lPage )
  167.             DumpPage( item -> lPage, lRecNo );
  168.  
  169.          if (bFound)
  170.          {
  171.             _xfree( cPage );
  172.             return;
  173.          }  
  174.          lNtxPos++;
  175.       }
  176.  
  177.       if ( item->lRecNo == lRecNo )
  178.       {
  179.          lRetVal = lNtxPos;
  180.          bFound = TRUE;
  181.          break;
  182.       }
  183.    }
  184.  
  185.    /* handle extra right pointer */
  186.    item = (ITEM *) &cPage[ uItemRef[ buffer -> uCount ] ];    
  187.    if (item -> lPage)
  188.       DumpPage(item -> lPage, lRecNo );
  189.  
  190.    _xfree( cPage );
  191.  
  192.    if ( bError )
  193.    {
  194.       DUMP_EXIT:
  195.       _xfree( cPage );
  196.       bError = TRUE;
  197.    }
  198. }
  199.  
  200. /**************************************************************************
  201.    FUNCTION: getNtxHandle ( n )
  202.    Returns the index handle for the current workarea, or -1 if error.
  203.    Brian Marasca 9/25/92
  204. **************************************************************************/
  205.  
  206. static int getNtxHandle ( n )
  207. int n ;
  208. {
  209.      extern char **_workareas ;
  210.      int **hp, han = -1 ;
  211.  
  212.      if(n > 0 && n <= 15)
  213.      {
  214.           hp = (int **) ((*_workareas) + 0x98 + ((n-1) * sizeof (char *))) ;
  215.           han = *hp ? **hp : -1 ;
  216.      }
  217.  
  218.      return han ;
  219. }
  220.  
  221.